package com.front.web.hotel;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.bootdo.common.utils.*;
import com.bootdo.haojiangboutils.MapBuild;
import com.bootdo.hotel.domain.HotelEveryDayMoney;
import com.bootdo.hotel.domain.HotelMember;
import com.bootdo.hotel.domain.HotelRoomType;
import com.bootdo.lvyou.domain.LvyouCity;
import com.bootdo.lvyou.domain.LvyouScenicSpot;
import com.front.web.commons.SendSms;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RequestMapping("/f/hotemember/v1/")
@RestController
public class HotelMemberControllerV1 extends  HotelSupperController{
    public static   String CODEKEY="SMS_176935039";
    public static   String SGINNAME="JDYD";

    /**
     * /f/hotemember/v1/get_member_info_by_mobile
     * @param mobile
     * @return
     */
    @RequestMapping("get_member_info_by_mobile")
    public R getMemberInfoByMobile(@RequestParam("mobile") String mobile){
        HotelMember hotelMember =  hotelMemberService.getMemberByMobile(mobile);
        if(null == hotelMember){
            return R.error("没有会员信息");
        }
        return R.ok().put("i", hotelMember);
    }



    /**
     *  /f/f/hotemember/v1/login
     * @param mobile
     * @return
     */
    @RequestMapping("login")
    public R login(@RequestParam("mobile") String mobile,@RequestParam("password")String password){
        if(null == redisUtil.get("DXCD_"+mobile)){
            return R.error("请先发送短信验证码");
        }
        String code = (String) redisUtil.get("DXCD_"+mobile);
        if(!code.equals(password)){
            return R.error("短信验证码错误");
        }
        HotelMember hotelMember =  hotelMemberService.getMemberByMobile(mobile);
        if(null == hotelMember){
            hotelMember = new HotelMember();
            hotelMember.setName("微会员");
            hotelMember.setIdCard("无");
            hotelMember.setMobile(mobile);
            hotelMemberService.save(hotelMember);
        }
        return R.ok().put("i", hotelMember);
    }



    /**
     *  /f/hotemember/v1/send/mobile/code
     * @param mobile
     * @return
     */
    @PostMapping("/send/mobile/code")
    public R sendMobileCode(@RequestParam("mobile") String mobile){
        if(null != redisUtil.get("DXLK_"+mobile)){
          return R.error("触发短信流控,请10秒后重试");
        }
        String code = EmailUtils.getRandomStr(4);
        Hog.e("{}短信验证码：{}",mobile,code);
        SendSmsResponse smsresponse =   SendSms.sendSms(mobile,code,SGINNAME, CODEKEY);
        if("OK".equals(smsresponse.getCode())){
            redisUtil.set("DXLK_"+mobile, "ok",10);
            redisUtil.set("DXCD_"+mobile, code,60*10);
            return  R.ok("验证码发送成功");
        }else {
            return  R.error(smsresponse.getMessage());
        }
    }




    /**
     *  /f/hotemember/v1/member_regist
     * @param mobile
     * @return
     */
    @RequestMapping("member_regist")
    public R memberRegist(@RequestParam("mobile") String mobile
            ,@RequestParam("name") String name,
                          @RequestParam("password") String password
                          /*@RequestParam("idCard") String idCard*/){
        HotelMember hotelMember =  hotelMemberService.getMemberByMobile(mobile);
        if(null != hotelMember){
            return R.error("该账号已经注册过了");
        }
        hotelMember = new HotelMember();
        hotelMember.setName(name);
        hotelMember.setIdCard("无");
        hotelMember.setMobile(mobile);
        hotelMember.setPassWord(password);
       /* hotelMember.setIdCard(idCard);*/
        hotelMemberService.save(hotelMember);
        return R.ok("注册成功").put("i", hotelMember);
    }



    /**
     *
     *
     *  会员信息修改
     *  /f/hotemember/v1/memberInfoUpdate
     * @param mobile
     * @return
     */
    @RequestMapping("memberInfoUpdate")
    public R memberInfoUpdate(@RequestParam("mobile")String mobile,
                              HttpServletRequest request
                              ){
        HotelMember hotelMember =  hotelMemberService.getMemberByMobile(mobile);
        if(null == hotelMember){
            return R.error("账号不存在");
        }
        String name = request.getParameter("name");
        if(StringUtils.isNotEmpty(name)){
            hotelMember.setName(name);
        }
        String pic = request.getParameter("pic");
        if(StringUtils.isNotEmpty(pic)){
            hotelMember.setPic(pic);
        }
        hotelMemberService.save(hotelMember);
        return R.ok("修改成功").put("i", hotelMember);
    }


}
